﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DAO
{
    public class DAO_LOPHOC
    {
        QLTTTHDataContext QLTTTH = new QLTTTHDataContext();
        public List<LOPHOC> LayThongTinKhoaHoc()
        {
            return QLTTTH.LOPHOCs.ToList();
        }
        public List<LSTHAYDOI> LayThongTinThayDoi()
        {
            return QLTTTH.LSTHAYDOIs.ToList();
        }
        public bool  ThemLopMoi(LOAILOP lop)
        {
                       
            try
            {
                lop.MALOAILOP = PhatSinhMaLoaiLop();
                QLTTTH.LOAILOPs.InsertOnSubmit(lop);
                QLTTTH.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public List<LOAILOP> LayThongTinLoaiLop()
        {
            return QLTTTH.LOAILOPs.ToList();
        }
        public bool CapNhatLop(LOAILOP lop)
        {
            try
            {
                var loailop = (from tv in QLTTTH.LOAILOPs
                               where tv.TENLOP == lop.TENLOP
                               select tv).SingleOrDefault();
                loailop.SOTIET = lop.SOTIET;
                loailop.HOCPHI = lop.HOCPHI;
                loailop.PHIGD = lop.PHIGD;
                QLTTTH.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public string PhatSinhMaLoaiLop()
        {
            string ma;
            int maloai = QLTTTH.LOAILOPs.Count();
            maloai = maloai + 1;
            if (maloai < 10)
            {
                ma = "00" + maloai.ToString();
            }
            else
            {
                ma = "0" + maloai.ToString();
            }
            return ma;
        }
        public bool LuuVet(LSTHAYDOI thaydoi)
        {
            try
            {
                QLTTTH.LSTHAYDOIs.InsertOnSubmit(thaydoi);
                QLTTTH.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
